<extend name="public/base"/>
<block name="style">
</block>
<block name="body">
<div class="layui-card qn-header">
	<div class="layui-breadcrumb">
		<a href="">首页</a>
		<a href=""><cite>数据备份</cite></a>
	</div>
</div>
<div class="layui-fluid">
	<div class="layui-card">
	<div class="layui-card-body">
	<div class="portlet-title">
		<div class="actions" >
				 <a id="export" class="layui-btn layui-btn-xs fbutton" href="javascript:;" autocomplete="off">立即备份</a>
		        <a id="optimize" class="layui-btn layui-btn-xs fbutton" href="{:U('optimize')}">优化表</a>
		        <a id="repair" class="layui-btn layui-btn-xs fbutton" href="{:U('repair')}">修复表</a>
		</div>
		<div class="clear"></div>
	</div>
	<div class="portlet-body">
	<div class="table-scrollable">
	<form id="export-form" method="post" action="{:U('export')}">
		<table class="layui-table" lay-even lay-skin="line">
			<thead>
			<tr>
			<th width="2%" class="check">
				<input type="checkbox" class="group-checkable">
			</th>
            <th>表名</th>
            <th width="120">数据量</th>
            <th width="120">数据大小</th>
            <th width="160">创建时间</th>
            <th width="160">备份状态</th>
            <th width="120">操作</th>
			</tr>
			</thead>
			<tbody>
				<volist name="list" id="table">
                        <tr>
                            <td class="check">
		                        <input class="ids" checked="checked" type="checkbox" name="tables[]" value="{$table.name}" />
		                      </td>
                            <td>{$table.name}</td>
                            <td>{$table.rows}</td>
                            <td>{$table.data_length|format_bytes}</td>
                            <td>{$table.create_time}</td>
                            <td class="info">未备份</td>
                            <td class="action">
                                <a class="ajax-get no-refresh layui-btn layui-btn-xs hbutton btn_see" href="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                                <a class="ajax-get no-refresh layui-btn layui-btn-xs hbutton btn_see" href="{:U('repair?tables='.$table['name'])}">修复表</a>
                            </td>
                        </tr>
                    </volist>
			</tbody>
			</table>
		</form>
		</div>
		<div class="layui-table-page">
		{$_page}
		</div>
	</div>
</div>
</div>
</div>
</block>

<block name="script">
<script>
var table;
 // handle group checkboxes check/uncheck
$('.group-checkable', table).change(function() {
    var set = $('tbody > tr > td:nth-child(1) input[type="checkbox"]', table);
    var checked = $(this).is(":checked");
    $(set).each(function() {
        $(this).attr("checked", checked);
    });
    //$.uniform.update(set);
});
var $form = $("#export-form"), $export = $("#export"), tables, $optimize = $("#optimize"), $repair = $("#repair");

$optimize.add($repair).click(function(){
	$.post(this.href, $form.serialize(), function(data){
		if(data.status){
			layer.msg(data.info, {icon: 1, fixed: true, offset: '80%', time:1000});
		} else {
			layer.msg(data.info, {icon: 2, fixed: true, offset: '80%', time:1000});
		}
		setTimeout(function(){
			$(that).removeClass('disabled').prop('disabled',false);
		},1500);
	}, "json");
	return false;
});

$export.click(function(){
	alert(1);
	$export.parent().children().addClass("disabled");
	$export.html("正在发送备份请求...");
	$.post(
		$form.attr("action"),
		$form.serialize(),
		function(data){
			if(data.status){
				tables = data.tables;
				$export.html(data.info + "开始备份，请不要关闭本页面！");
				backup(data.tab);
				window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
			} else {
				layer.msg(data.info, {icon: 2, fixed: true, offset: '80%', time:1000});
				$export.parent().children().removeClass("disabled");
				$export.html("立即备份");
				setTimeout(function(){
					$(that).removeClass('disabled').prop('disabled',false);
				},1500);
			}
		},
		"json"
	);
	return false;
});

function backup(tab, status){
	status && showmsg(tab.id, "开始备份...(0%)");
	$.get($form.attr("action"), tab, function(data){
		if(data.status){
			showmsg(tab.id, data.info);

			if(!$.isPlainObject(data.tab)){
				$export.parent().children().removeClass("disabled");
				$export.html("备份完成，点击重新备份");
				window.onbeforeunload = function(){ return null }
				return;
			}
			backup(data.tab, tab.id != data.tab.id);
		} else {
			layer.msg(data.info, {icon: 2, fixed: true, offset: '80%', time:1000});
			$export.parent().children().removeClass("disabled");
			$export.html("立即备份");
			setTimeout(function(){
				//$('.modal-footer').find('button').click();
				$(that).removeClass('disabled').prop('disabled',false);
			},1500);
		}
	}, "json");

}

function showmsg(id, msg){
	$form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
}
</script>
</block>
<block name="jquery_script">
</block>